<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
    class="page-nav-bar"
    title="个人信息"
    left-arrow @click-left="$router.back()" />
    <!-- /导航栏 -->

    <input type="file"
     hidden
      ref="inputFile"
      @change="inputChange"
      >

    <van-cell
     title="头像"
      is-link
      @click="$refs.inputFile.click()"
      >
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
      />
    </van-cell>
    <van-cell title="昵称"
     :value="user.name" is-link
     @click="isUpdateNameShow = true"
      />
    <van-cell
     title="性别"
      :value="user.gender === 0 ? '男' : '女'"
       is-link
       @click="isUpdateGenderShow = true"
        />
    <van-cell title="生日"
     :value="user.birthdy || '1977-00-00'"
      is-link
      @click="isUpdateBirthdayShow = true"

       />

    <van-popup
     v-model="isUpdateNameShow"
      style="height: 100%;"
      position="bottom"
      >
      <update-name
      v-if="isUpdateNameShow"
      v-model="user.name"
       @close="isUpdateNameShow = false"
       />
    </van-popup>

     <van-popup
     v-model="isUpdateGenderShow"
      position="bottom"
      >
       <update-gender
       v-if="isUpdateGenderShow"
       v-model="user.gender"
       @close="isUpdateGenderShow = false"
       />
    </van-popup>

      <van-popup
     v-model="isUpdateBirthdayShow"
      position="bottom"
      >
      <update-birthday
      v-if="isUpdateBirthdayShow"
      v-model="user.birthday"
      @close="isUpdateBirthdayShow = false"
      />
    </van-popup>

    <!-- 编辑头像弹层 -->
    <van-popup
    v-model="isShowUpdateAvatar"
    style="height: 100%"
    position="bottom">
      <update-avatar
       v-if="isShowUpdateAvatar"
       :img="img"
       @close="isShowUpdateAvatar = false"
       @update-avatar="user.photo = $event"
        />
    </van-popup>
    <!-- 编辑头像弹层 -->
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import updateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdateAvatar from './components/update-avatar'
export default {
  data () {
    return {
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isShowUpdateAvatar: false,
      img: null
    }
  },
  components: {
    updateName,
    UpdateGender,
    UpdateBirthday,
    UpdateAvatar
  },
  created () {
    this.loadProfile()
  },

  methods: {
    async loadProfile () {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    inputChange () {
      const file = this.$refs.inputFile.files[0]
      this.img = window.URL.createObjectURL(file)
      this.isShowUpdateAvatar = true
      this.$$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
  background: #f5f7f9;
}
}
</style>
